layui.use(['form','table'], function(){
	var form = layui.form;
	var table2=layui.table;
	
	/*家长学生关系下拉框(父亲，母亲)*/
	$.ajax({
		type : 'get',
		url : '../json/stuParRelationship.json',
		datatype : 'json',
		success : function(relationdata) {
			if (relationdata.code == 0) {
				var str = "";
				for(var i = 0; i < relationdata.count; i++){
					str += '<option value=' + relationdata.data[i].name + '>'+ relationdata.data[i].name+'</option>';
				}
				$("#relationship").append(str);
				form.render("select");
			}
			else{
				layer.msg("家长学生关系信息获取失败！");
			}
		}
	});
	
	/*加载学校类型下拉框*/
	$.ajax({
		type : 'get',
		url : '../json/schoolType.json',
		datatype : 'json',
		success : function(schooltypedate) {
			if (schooltypedate.code == 0) {
				var str = "";
				for(var i = 0; i < schooltypedate.count; i++){
					str += '<option value=' + schooltypedate.data[i].id + '>'+ schooltypedate.data[i].name+'</option>';
				}
				$("#seladdschooltype").append(str);
				form.render("select");
			}
		}
	});
	
	/*点击学校类型下拉框*/
	form.on('select(seladdschooltype)', function(data) {
		var schooltype = data.value;
		if(schooltype=="00" || schooltype==null || schooltype=='null'){
			schooltype="";
		}
		$("#seladdschool").html("");
		//根据阶段筛选学校
		$.ajax({
			type : 'get',
			url : '../sysschoolmanagement/getschoolbywhere?schooltype='+schooltype,
			datatype : 'json',
			success : function(schooldata) {
				if (schooldata.code == 0) {
					var str = "<option value='00'>请选择学校</option>";
					for(var i = 0; i < schooldata.count; i++){
						str += '<option value=' + schooldata.data[i].schoolid + '>'+ schooldata.data[i].schoolName +'</option>';
					}
					$("#seladdschool").append(str);
					form.render("select");
				}
				else{
					layer.msg('暂无对应的学校', {icon: 2});
					$("#seladdschool").html("");
					form.render("select");
				}
				
			}
		});

	});

	/*动态加载班级下拉框*/
	form.on('select(selgradename)', function(data) {
		var schoolid = $("#selschool").val();
		var gradname=data.value;
		if(schoolid==00 || schoolid=='null' || schoolid==null){
			layer.msg("请选择学校");
			return;
		}
		if(gradname==00 || gradname=='null' || gradname==null){
			gradname="";
		}
		$("#classid").html("");
		$.ajax({
			type : 'get',
			url : '../sysclassmanagement/getclassbyschoolandgrade?schoolid=' + schoolid+'&gradename='+gradname,
			datatype : 'json',
			success : function(classdata) {
				if (classdata.code == 0) {
					var str = "<option value='00'>请选择班级</option>";
					for(var i = 0; i < classdata.count; i++){
						str += '<option value=' + classdata.data[i].classid + '>'+classdata.data[i].className +'</option>';
					}
					$("#classid").append(str);
					form.render();
				}
				else{
					layer.msg('班级信息获取失败', {icon: 2});
					$("#classid").html("");
					form.render("select");
				}
			}
		});
	});
	
	//清空所选学生
	$("#clearstudent").click(function(){
		$("#changestulistid").val("");
		$("#changestulistname").val("");
	});
	
	/*点击查询学生信息(选择家长学生)*/
	$("#selStuBtn").click(function(){
		var schoolid=$("#selschool").val();
		var gradename=$("#selgradename").val();
		var classid = $("#classid").val();
		var userName = $("#idorrelname").val().trim();
		if(schoolid==00 || schoolid==null || schoolid=='null' || schoolid==undefined){
			schoolid="";
		}
		if(gradename==00 || gradename==null || gradename=='null' || gradename==undefined){
			gradename="";
		}
		if(classid==00 || classid==null || classid=='null' || gradename==undefined){
			classid="";
		}
		
		/*添加家长选择学生*/
		table2.render({
			elem: '#parentStus',
			id:'parentStus',
			url: '../studentmanagement/getstudentwhere?idorrelname='+userName+'&classid='+classid+'&gradename='+gradename+"&schoolid="+schoolid,
			title: '学生数据表',
			height:'300',
			skin: 'line',
			even: true,
			cols: [
				[{
					field: 'stuid',
					title: '账号',
					align: "center",
				}, {
					field: 'realName',
					title: '真实姓名',
					align: "center",
				}, {
					field: 'sex',
					title: '性别',
					align: "center",
				},{
					field: 'gradename',
					title: '所属年级',
					align: "center",
				},{
					field: 'className',
					title: '所属班级',
					align: "center",
				}, {
					field: 'gradeYear',
					title: '入学年份'	,
					align: "center",
				},{
					title: '操作',
					toolbar: '#stuBar',
					align: "center",
					width:'100'
				}]
			]
		});

	});
	
	//点击选择学生
	$("#btnadstu").click(function(){
		$("#changestulistid").val("");
		$("#changestulistname").val("");
		index = layer.open({
	        type: 1, 
	        title: '选择学生',
	        shade: 0.8,
	        maxmin:true,
	        content: $('#parentlayercon'),
	        cancel: function(){ 
	        	$("input").val('');
			   $(".relationLayer-con").css("display","none");
			}
	    });
		layer.full(index);	
	});
	
	//点击选择学生为家长选择一个学生
	table2.on('tool(parentStus)', function(obj){
	    var data = obj.data; //获得当前行数据
		if(obj.event === 'selstudent') {
			var changestulistname=$("#changestulistname").val();
			var changestulistid=$("#changestulistid").val();
			if(changestulistname==null || changestulistname==""){
				var studentname=data.realName+"--"+data.stuid+"("+data.gradename+data.className+")";
				var newchangestulistname=studentname;
				var studentid=data.stuid;
				var newchangestulistid=studentid;
				$("#changestulistid").val(newchangestulistid);
				$("#changestulistname").val(newchangestulistname);
			}else{
				if(changestulistid.indexOf(data.stuid) >= 0 ) { 
					 layer.alert('已选择该学生！', {icon: 6});
				    return;
				} else{
					var studentname=data.realName+"--"+data.stuid+"("+data.gradename+data.className+")";
					var newchangestulistname=changestulistname+","+studentname;
					var studentid=data.stuid;
					var newchangestulistid=changestulistid+","+studentid;
					$("#changestulistid").val(newchangestulistid);
					$("#changestulistname").val(newchangestulistname);
				}
				
			}
		}
    });
	
	//点击保存，将学生信息加载到输入框
	$("#btnaddstulist").click(function(){
		var changestulistid= $("#changestulistid").val();
		var changestulistname= $("#changestulistname").val(); 
		$("#parentstudentid").val(changestulistid);
		$("#parentstudent").val(changestulistname);
		layer.closeAll();
		$(".relationLayer-con").hide();
	});
	
	/*点击保存*/
	form.on('submit(addParent)', function(pardata){
		var index = layer.load(0, {
			shade: 0.1
		});
		var schoolid=$("#seladdschool").val();
		var realname=$("#realname").val();
		var sex=$("input[name='sex']:checked").val();
		var phone=$("#phone").val();
		var studentlistid=$("#parentstudentid").val();
		var relationship=$("#relationship").val();
		if(schoolid==null || schoolid=="" || schoolid=='null' || schoolid==undefined){
			layer.close(index);
			layer.msg("请选择所属学校");
			return;
		}
		if(studentlistid==null || studentlistid==""){
			layer.close(index);
			layer.msg("请选择学生");
			return;
		}
		if(relationship==null || relationship=="00" || relationship==""){
			layer.close(index);
			layer.msg("请选择与学生关系");
			return;
		}
		$.ajax({
			url: '../sysparentmanagement/addParent',  
	        type: 'get', 
	        dataType: 'json',  
	        data:{realName:realname,sex:sex,phone:phone,schoolid:schoolid,
	        	studentlistid:studentlistid,relationship:relationship},
	        success: function (getdata) {
	          if (getdata.code == 10001) {
	        	  layer.close(index);
	        	  layer.confirm('家长信息添加成功！', {
					  btn: ['确定']
				  }, function(){
					  window.location.href = "../parentManagement/editParent.jsp";
					  $("#liformtitle").text("家长信息查看");
				  });
	          }else { 
	        	  layer.close(index);
	        	  layer.alert('家长信息添加失败！', {icon: 2});
	        	  return false;
	          }  
	        },
	        error:function(){ 
	        	layer.close(index);
	        	layer.alert('家长信息添加失败！', {icon: 2});
	        }
		});
	});
	
});